/*
 * Node.java
 *
 * Created on 8 de abril de 2006, 12:22
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package fmap.model;

import fmap.DatosCompartidos;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.ImageIcon;

/**
 *
 * @author javi
 */
public class Node
{
    
    int idNode;
    int idMapF;
    int idParent;
    int idCollection;
    String name;
    String desc;
    String keyWord;
    String icon = "";
    long size;
    String path;
    int type; // 0=> directorio, 1=> fichero
    Date date;
    int idTable;
    int idTableSub;
    int idTableParent;
    Node[] nodesHijos = null;
    String nameFound = "0";
    String ext="";
    
    boolean subNodosCargados = false; // Nos indica si ya se han cargado desde la BD los nodos hijos del nodo actual
    
    
    
    /** Creates a new instance of Node */
    public Node ()
    {
    }
    
    public Node ( int _idNode, int _idMapF, int _idParent, int _idCollection, String _name,
            String _icon, long _size, String _path, int _type, java.sql.Date _date, String _keyWord, String _descrip, String _nameFound, 
            int _idTable, int _idTableParent, int _idTableSub)
    {
        
        idNode          =   _idNode;
        idMapF          =   _idMapF;
        idParent        =   _idParent;
        idCollection    =   _idCollection;
        name            =   _name;
        icon            =   _icon;
        size            =   _size;
        path            =   _path;
        type            =   _type;
        keyWord         =   _keyWord;
        desc            =   _descrip;
        date            =   new java.util.Date ( _date.getTime () );
        nameFound       =   _nameFound;
        idTable         =   _idTable;
        idTableParent   =   _idTableParent;
        idTableSub      =   _idTableSub;
        
        // Si es un fichero averiguamos su extension para despues mostrar su icono correspondiente
        if( type == 1 )
        {
            int posExt = name.lastIndexOf (".");
            ext = name.substring (posExt+1, name.length ());
        }
        
    }
    
    public Node ( int _idNode, int _idMapF, int _idParent, int _idCollection, String _name,
            String _icon, long _size, String _path, int _type, java.sql.Date _date, String _keyWord, String _descrip, 
            int _idTable, int _idTableParent, int _idTableSub )
    {
        
        idNode          =   _idNode;
        idMapF          =   _idMapF;
        idParent        =   _idParent;
        idCollection    =   _idCollection;
        name            =   _name;
        icon            =   _icon;
        size            =   _size;
        path            =   _path;
        type            =   _type;
        keyWord         =   _keyWord;
        desc            =   _descrip;
        date            =   new java.util.Date ( _date.getTime () );
        idTable         =   _idTable;
        idTableParent   =   _idTableParent;
        idTableSub      =   _idTableSub;

        // Si es un fichero averiguamos su extension para despues mostrar su icono correspondiente
        if( type == 1 )
        {
            int posExt = name.lastIndexOf (".");
            ext = name.substring (posExt+1, name.length ());
        }
    }
    
    
    public Node ( int _idMapF, int _idParent, int _idCollection, String _name,
            String _icon, long _size, String _path, int _type, java.sql.Date _date, 
            int _idTable, int _idTableParent, int _idTableSub )
    {
        
        idMapF          =   _idMapF;
        idParent        =   _idParent;
        idCollection    =   _idCollection;
        name            =   _name;
        icon            =   _icon;
        size            =   _size;
        path            =   _path;
        type            =   _type;
        date            =   new java.util.Date ( _date.getTime () );
        idTable         =   _idTable;
        idTableParent   =   _idTableParent;
        idTableSub      =   _idTableSub;
        
        // Si es un fichero averiguamos su extension para despues mostrar su icono correspondiente
        if( type == 1 )
        {
            int posExt = name.lastIndexOf (".");
            ext = name.substring (posExt+1, name.length ());
        }
    }


    public int getIdNode ()
    {
        return idNode;
    }
    
    public int getIdMapF ()
    {
        return idMapF;
    }
    
    public int getIdParent ()
    {
        return idParent;
    }
    
    public int getIdCollection ()
    {
        return idCollection;
    }
    
    public String getName ()
    {
        return name;
    }
    
    public String getIcon ()
    {
        if( type == 0 )
        {
            if( icon == null )          return "/fmap/img/g_folder.png";
            else if( icon.equals ("") ) return "/fmap/img/g_folder.png";
            else                        return icon;
        }
        else
        {
            if( icon == null || icon.equals ("") )
            {
//                return "/fmap/img/file.png";
                return "/fmap/img/mimetypes/"+DatosCompartidos.getIconMime (ext);
            }
            else                        return icon;
            //AQUI TENEMOS QUE DEVOLVER ICONO PERSONALIZADO O SINO EN BASE A SU EXTENSION
        }
    }
    
    public void setIcon ( String _icon )
    {
        icon = _icon;
    }
    
    
    public String getSizeString ()
    {
        int sizeFormat;
        
        sizeFormat = (int) size;
        
        
        if( sizeFormat < 1024 )
        {
            return new Integer ( sizeFormat ).toString ()+" Bytes";
        }
        else if( sizeFormat > 1024 )
        {
            sizeFormat = sizeFormat / 1024;
            return new Integer ( sizeFormat ).toString ()+" KB";
        }
        
        return "";
    }
    
    
    public String getKeyWord ()
    {
        return keyWord;
    }
    
    
    public void setKeyWord ( String _keyWord )
    {
        keyWord = _keyWord;
    }
    
    public String getDescrip ()
    {
        return desc;
    }
    
    public void setDescrip ( String _desc )
    {
        desc = _desc;
    }
    
    
    public long getSize ()
    {
        return size;
    }
    
    public String getPath ()
    {
        return path;
    }
    
    
    /*
     * type = 0 => directorio
     * type = 1 => fichero
     */
    public int getType ()
    {
        return type;
    }
    
    
    public String getDateString ()
    {
        SimpleDateFormat dateFormat = new SimpleDateFormat ("dd/MM/yyyy");
        
        return dateFormat.format ( date );
    }
    
    
    public Date getDate ()
    {
        return date;
    }
    
    public java.sql.Date getDateSql ()
    {
        java.sql.Date sqlDate = new java.sql.Date (date.getTime ());
        
        return sqlDate;
    }
    
    public void setId ( int _idNode )
    {
        idNode = _idNode;
    }
    
    public String toString ()
    {
        return name;
    }

    /**
     * Obtenemos el listado de nodos para utilizar en el control "table"
     */
    public Vector getVNodeTable ()
    {
        Vector v = new Vector ();
        
        if( nodesHijos != null )
        {
            for(int i=0; i<nodesHijos.length; i++)
            {
                Vector vFila = new Vector ();
                
                vFila.add ( nodesHijos[i] );
                vFila.add ( nodesHijos[i].getSizeString () );
                vFila.add ( nodesHijos[i].getDateString () );
                
                v.add ( vFila );
            }
        }
        
        return v;
    }
    
    
    public Vector getVNodeType ( int type )
    {
        Vector v = new Vector ();
        
        if( nodesHijos != null )
        {
            for(int i=0; i<nodesHijos.length; i++)
            {
//                dirs[i].setEnlaceAPadre (false);
                if( nodesHijos[i].getType () == type )  v.add (nodesHijos[i]);
            }
        }
        else if( subNodosCargados == true ) return null;
        
        return v;
    }
    
    
    public Vector getVNode ()
    {
        Vector v = new Vector ();
        
        if( nodesHijos != null )
        {
            for(int i=0; i<nodesHijos.length; i++)
            {
//                dirs[i].setEnlaceAPadre (false);
                v.add (nodesHijos[i]);
            }
        }
        else if( subNodosCargados == true ) return null;
        
        return v;
    }
    
    
    public void setNode (Vector v)
    {
        Node[] aNode = new Node[v.size ()];
        
        for( int i=0; i<v.size (); i++)
        {
            aNode[i] = (Node)v.get (i);
        }
        
        // Indicamos que los nodos ya han sido cargados
        subNodosCargados = true;
        
        nodesHijos = aNode;
    }
    
    
    public String getNameFound ()
    {
        return nameFound;
    }
    
    public int getIdTable ()
    {
        return idTable;
    }
    
    public int getIdTableParent ()
    {
        return idTableParent;
    }

    public int getIdTableSub ()
    {
        return idTableSub;
    }


    public String getExtension()
    {
        return ext;
    }

}
